﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class sysadmin_CountManager_UserLoggin : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerSecurityUserInfo objsecurityuserinfologic = new ManagerSecurityUserInfo();
    //ManagerDepartment objdepartmentlogic = new ManagerDepartment();
  
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.CountLogin))
        //{
        //    Script.ShowAndHistory("您没有用户登录统计的权限!", -1);
        //    return;
        //}
        //if (!IsPostBack)
        //{
        //    DataBinderDepartment();
        //    string search = GetSearch();
        //    BinderPager(search);
        //    BinderGV(search, 1);
        //}
    }
    private string GetSearch()
    {
        string _username = txt_username.Text.Trim();
        string _departmentid = dpdlst_DepartmentName.SelectedValue;
        StringBuilder search = new StringBuilder();
        if (!string.IsNullOrEmpty(_username))
        {
            search.Append(" UserName like '%" + _username + "%' and ");
        }
        if (_departmentid != "0")
        {
            search.Append(string.Format(" DepartmentID={0} and", _departmentid));
        }
        search.Append(" 1=1 ");        
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        //AspNetPager1.RecordCount = objsecurityuserinfologic.Count(search);
        //AspNetPager1.PageSize = FBasePage.PageSize();
    }

    private void BinderGV(string search, int pageIndex)
    {
        //DataTable dt = new DataTable();
        //dt = objsecurityuserinfologic.Query(search, FBasePage.PageSize(), pageIndex);
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    if (dt.Rows[i]["LoginCount"].ToString().Trim() == "")
        //    {
        //        dt.Rows[i]["LoginCount"] = "0";
        //    }
        //}
        //    ViewState["dt"] = dt;
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
    }

    #region 树形结构的部门
    private void DataBinderDepartment()
    {
        //DataTable dt = objdepartmentlogic.Query("");
        //DataRow dr = dt.NewRow();
        //BinderDepartment(dt, 0);

        //ListItem li = new ListItem();
        //li.Text = "不限部门";
        //li.Value = "0";
        //dpdlst_DepartmentName.Items.Insert(0, li);
    }
    private void BinderDepartment(DataTable dt, int parentdepartmentid)
    {
        //该方法被BinderCatalog()调用
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentDepartmentID=" + parentdepartmentid.ToString();  //过滤
        ListItem li = new ListItem();
        int layer = 0;
        foreach (DataRowView drv in dv)
        {
            layer = int.Parse(drv["Level"].ToString().Trim());
            string span = "";
            if (layer != 0)
            {
                for (int j = 0; j < layer; j++)
                {
                    span += "　";
                }
                span += "|-";
            }

            li = new ListItem();
            li.Text = span + drv["DepartmentName"].ToString();
            li.Value = drv["DepartmentID"].ToString();
            dpdlst_DepartmentName.Items.Add(li);
            BinderDepartment(dt, int.Parse(drv["DepartmentID"].ToString()));
        }
    }
    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {      
        //string search = GetSearch();
        //BinderPager(search);
        //BinderGV(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);  
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        
        //DataTable dt = (DataTable)ViewState["dt"];
        //ExportList(dt);

    }
    private void ExportList(DataTable dt)
    {
        //ArrayList list = new ArrayList();
        //for (int i = 0; i < dt.Columns.Count; i++)
        //{
        //    if (dt.Columns[i].ColumnName == "UserID")
        //    {
        //        dt.Columns[i].ColumnName = "用户ID";
        //        dt.Columns[i].SetOrdinal(0);
        //    }
        //    else if (dt.Columns[i].ColumnName == "UserName")
        //    {
        //        dt.Columns[i].ColumnName = "帐户";
        //        dt.Columns[i].SetOrdinal(1);
        //    }
        //    else if (dt.Columns[i].ColumnName == "RealName")
        //    {
        //        dt.Columns[i].ColumnName = "真实姓名";
        //        dt.Columns[i].SetOrdinal(2);
        //    }
        //    else if (dt.Columns[i].ColumnName == "LoginCount")
        //    {
        //        dt.Columns[i].ColumnName = "登陆次数";
        //        dt.Columns[i].SetOrdinal(3);
        //    }
        //    else if (dt.Columns[i].ColumnName == "LastLoginDate")
        //    {
        //        dt.Columns[i].ColumnName = "最后登陆时间";
        //        dt.Columns[i].SetOrdinal(4);
        //    }
        //    else
        //    {
        //        list.Add(dt.Columns[i].ColumnName);

        //    }
        //}
        //for (int i = 0; i < list.Count; i++)
        //{
        //    dt.Columns.Remove(list[i].ToString());
        //}
        //Excel objExcel = new Excel();
        //objExcel.ToExcel(dt, this.Page, "用户登陆统计", "用户登陆统计信息", "UserLoginInfo");
    }
    protected void btnExport_All_Click(object sender, EventArgs e)
    {
        //string search = GetSearch();
        //DataTable dt = new DataTable();
        //dt = objsecurityuserinfologic.Query(search, objsecurityuserinfologic.Count(search), 1);
        //ExportList(dt);

    }
}
